package com.onlyphoto.onlymall.mvp.view.fragment

import android.content.Context
import android.graphics.Color
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseFragment

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.onlyphoto.onlymall.bean.response.CategoryBean
import com.onlyphoto.onlymall.bean.response.SingleActivityBean
import com.onlyphoto.onlymall.mvp.contract.CCategory
import com.onlyphoto.onlymall.mvp.presenter.PCategoryImpl
import com.onlyphoto.onlymall.mvp.view.binder.SecondaryBinder
import com.onlyphoto.onlymall.widget.verticalTabLayout.BTabView
import com.onlyphoto.onlymall.widget.verticalTabLayout.IOnlyTabView
import com.onlyphoto.onlymall.widget.verticalTabLayout.MyTabLayout
import com.onlyphoto.onlymall.widget.verticalTabLayout.OnlyTabAdapter
import kotlinx.android.synthetic.main.fragment_category.*
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class CategoryFragment : BaseFragment<PCategoryImpl>(), CCategory.IVCategory {

    var adapter: MultiTypeAdapter? = null
    var items: Items? = null
    var layoutManager: GridLayoutManager? = null
    private var cart2Id: String? = null
    var mList: ArrayList<CategoryBean>? = null
    var cIndex = 0
    var bean: SingleActivityBean? = null

    override fun retry() {
        loadData()
    }

    override fun init() {
        initRv()
        loadData()
        initListener()
    }

    fun initListener() {
        single_activity.setOnClickListener{
            EventBus.getDefault().post(bean)
        }
    }

    private fun initRv() {
        adapter = MultiTypeAdapter()
        layoutManager = GridLayoutManager(activity, 3)
        adapter?.register(CategoryBean::class.java, SecondaryBinder())
        items = Items()
        adapter?.items = items!!
        rv_category.layoutManager = layoutManager
        rv_category.adapter = adapter
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        EventBus.getDefault().register(this)

    }

    override fun requestActivitySingleSuccess(bean: SingleActivityBean) {
        Glide.with(requireActivity()).load(bean.img_url).placeholder(R.mipmap.img_default).into(single_activity)
        this.bean = bean
    }

    override fun getCategorySuccess(list: ArrayList<CategoryBean>) {
        mList = list
        mtl_tab.setTabAdapter(object : OnlyTabAdapter {
            override fun getCount(): Int {
                return list.size
            }

            override fun getIcon(position: Int): IOnlyTabView.TabIcon? {
                return null
            }

            override fun getTitle(position: Int): IOnlyTabView.TabTitle? {
                return IOnlyTabView.TabTitle.Builder()
                        .setContent(list[position].name)
                        .setTextColor(Color.RED, Color.DKGRAY)
                        .build()
            }

            override fun getBackground(position: Int): Int {
                return 0
            }
        })

        mtl_tab.addOnTabSelectedListener(object : MyTabLayout.OnTabSelectedListener{
            override fun onTabSelected(tab: BTabView?, position: Int) {
                mPresenter?.getCategory(2, list[position].id)
                mPresenter?.requestActivitySingle(2, Integer.parseInt(list[position].id))

            }

            override fun onTabReselected(tab: BTabView?, position: Int) {
            }

        })
    }

    override fun getSecondarySuccess(list: ArrayList<CategoryBean>) {
        if (items?.size!! > 0) {
            items?.clear()
        }
        items?.addAll(list)
        adapter?.notifyDataSetChanged()
    }

    fun loadData() {
        mPresenter?.getCategory(1)
    }

    override fun getLayoutRes(): Int {
        return R.layout.fragment_category
    }

    override fun createPresenter() {
        mPresenter = PCategoryImpl(mContext, this)
    }

    override fun showLoading() {}

    override fun hideLoading() {}

    companion object {

        fun newInstance(): CategoryFragment {
            val fragment = CategoryFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(cart2Id: String) {
        this.cart2Id = cart2Id
    }



    override fun onDetach() {
        super.onDetach()
        EventBus.getDefault().unregister(this)
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden && mList != null) {
            for ((index, bean) in mList!!.withIndex()){
                if (bean.id == cart2Id) {
                    cIndex = index
                }
            }
            mPresenter?.getCategory(2, mList!![cIndex].id)
            mPresenter?.requestActivitySingle(2, Integer.parseInt(mList!![cIndex].id))
            mtl_tab.setTabSelected(cIndex)
        }
    }
}
